package com.probox.modules.controller.product;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.product.ProductGroup;
import com.probox.common.entity.service.product.IProductGroupService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.product.ProductUnit;
import com.probox.common.entity.service.product.IProductUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品单位Controller
 *
 *
 * @date 2021-05-27
 */
@RestController
@RequestMapping("/product/unit")
public class ProductUnitController extends BaseController {
    @Autowired
    private IProductUnitService productUnitService;

    @Autowired
    private IProductGroupService productGroupService;

    /**
     * 查询商品单位列表
     */
    @PreAuthorize(hasPermi = "product:unit:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductUnit productUnit) {
        startPage();
        List<ProductUnit> list = productUnitService.queryList(productUnit);
        TableDataInfo rst = getDataTable(list);
        ProductGroup productGroup = new ProductGroup();
        productGroup.setDataStatus(DataStatus.valid.getKey());
        productGroup.setParentGroupUid(0L);
        List<ProductGroup> groups = productGroupService.queryList(productGroup);
        Map<Long,String> groupMap = new HashMap<>();
        for (ProductGroup group:groups){
            groupMap.put(group.getGroupUid(),group.getGroupName());
        }
        for(ProductUnit unit:list){
            if (unit.getGroupUid() != null){
                unit.setGroupName(groupMap.get(unit.getGroupUid()));
            }
        }
        return rst;
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/bulidSelect")
    public AjaxResult bulidSelect() {
        return AjaxResult.success(productUnitService.bulidSelect());
    }

    /**
     * 导出商品单位列表
     */
    @PreAuthorize(hasPermi = "product:unit:export")
    @Log(title = "论文折扣", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductUnit productUnit) throws IOException {
        List<ProductUnit> list = productUnitService.queryList(productUnit);
        ExcelUtil<ProductUnit> util = new ExcelUtil<ProductUnit>(ProductUnit.class);
        util.exportExcel(response, list, "论文折扣数据");
    }

    /**
     * 获取商品单位详细信息
     */
    @PreAuthorize(hasPermi = "product:unit:query")
    @GetMapping(value = "/{unitUid}")
    public AjaxResult getInfo(@PathVariable("unitUid") Long unitUid) {
        return AjaxResult.success(productUnitService.selectProductUnitById(unitUid));
    }

    /**
     * 新增商品单位
     */
    @PreAuthorize(hasPermi = "product:unit:add")
    @Log(title = "论文折扣", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductUnit productUnit) {
        if (productUnit.getDiscount().compareTo(new BigDecimal(1))>0){
            throw new RuntimeException("折扣不能大于1");
        }
        if (productUnit.getDiscount().compareTo(new BigDecimal(0.1))<0){
            throw new RuntimeException("折扣不能小于0.1");
        }
        return toAjax(productUnitService.insertProductUnit(productUnit));
    }

    /**
     * 修改商品单位
     */
    @PreAuthorize(hasPermi = "product:unit:edit")
    @Log(title = "论文折扣", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductUnit productUnit) {
        if (productUnit.getDiscount().compareTo(new BigDecimal(1))>0){
            throw new RuntimeException("折扣不能大于1");
        }
        if (productUnit.getDiscount().compareTo(new BigDecimal(0.1))<0){
            throw new RuntimeException("折扣不能小于0.1");
        }
        return toAjax(productUnitService.updateProductUnit(productUnit));
    }

    /**
     * 删除商品单位
     */
    @PreAuthorize(hasPermi = "product:unit:remove")
    @Log(title = "论文折扣", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitUids}")
    public AjaxResult remove(@PathVariable Long[] unitUids) {
        return toAjax(productUnitService.deleteProductUnitByIds(unitUids));
    }
}
